package main

import (
	"fmt"
	"gitee.com/frank2020/joy4/av"
	"gitee.com/frank2020/joy4/av/avutil"
	"gitee.com/frank2020/joy4/format"
	"gitee.com/frank2020/joy4/format/flv"
	"os"
)

func init() {
	format.RegisterAll()
}

func main() {
	rtmp, err := avutil.Open("rtmp://192.168.50.211/test/1")
	if err != nil {
		fmt.Println("%v\n", err)
		return
	}

	streams, err := rtmp.Streams()

	if err != nil {
		fmt.Println("%v\n", err)
		return
	}

	// 流信息
	for _, stream := range streams {
		if stream.Type().IsAudio() {
			astream := stream.(av.AudioCodecData)
			fmt.Println(astream.Type(), astream.SampleRate(), astream.SampleFormat(), astream.ChannelLayout())
		} else if stream.Type().IsVideo() {
			vstream := stream.(av.VideoCodecData)
			fmt.Println(vstream.Type(), vstream.Width(), vstream.Height())
		}
	}

	// rtmp流保存文件

	file, err := os.Create("./1.flv")
	if err != nil {
		fmt.Println("%v\n", err)
		return
	}

	defer file.Close()

	muxer := flv.NewMuxer(file)

	// 文件头和文件尾巴
	muxer.WriteHeader(streams)
	defer muxer.WriteTrailer()

	pkt, err := rtmp.ReadPacket()
	for err == nil {
		muxer.WritePacket(pkt)
		pkt, err = rtmp.ReadPacket()
	}

}
